package com.pj.project.file_info;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.http.server.HttpServerResponse;
import com.pj.project.util.SeatCache;
import com.pj.project4sp.uploadfile.UploadConfig;
import com.pj.project4sp.uploadfile.UploadUtil;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import com.pj.current.satoken.StpUserUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.pj.project4sp.uploadfile.UploadUtil.uploadConfig;


/**
 * Controller: file_info -- 升级信息
 * @author xxxxxx
 */
@RestController
@RequestMapping("/FileInfo/")
public class FileInfoController {




	/** 底层 Service 对象 */
	@Autowired
	FileInfoService fileInfoService;

	/** 增 */
	@RequestMapping("add")
	@SaCheckPermission(FileInfo.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(FileInfo f){
		String name = f.getUrl().substring(f.getUrl().lastIndexOf("/") + 1);
		if(name.equals("") || null == name){
			return AjaxJson.getError("请检查上传文件后，重新上传");
		}
		if(null == f.getType() || f.getType() == 0){
			f.setType(1);
		}

		f.setName(name);
		f.setRootFolder(uploadConfig.rootFolder + uploadConfig.httpPrefix + uploadConfig.fileFolder);
		fileInfoService.add(f);
		UploadUtil.seatCache.remove(name);
		return AjaxJson.getSuccessData(f);
	}

	/** 删 */
	@RequestMapping("delete")
	@SaCheckPermission(FileInfo.PERMISSION_CODE)
	public AjaxJson delete(Integer id){

		int line = fileInfoService.delete(id);

		return AjaxJson.getByLine(line);
	}

	/** 删 - 根据id列表 */
	@RequestMapping("deleteByIds")
	@SaCheckPermission(FileInfo.PERMISSION_CODE)
	public AjaxJson deleteByIds(){

		return AjaxJson.getError("批量删除暂不可用！");
//		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
//		int line = SP.publicMapper.deleteByIds(FileInfo.TABLE_NAME, ids);
//		return AjaxJson.getByLine(line);
	}

	/** 改 */
	@RequestMapping("update")
	@SaCheckPermission(FileInfo.PERMISSION_CODE)
	public AjaxJson update(FileInfo f){
		int line = fileInfoService.update(f);
		return AjaxJson.getByLine(line);
	}

	/** 查 - 根据id */
	@RequestMapping("getById")
	@SaCheckPermission(FileInfo.PERMISSION_CODE)
	public AjaxJson getById(Integer id){


		BufferedImage generate = QrCodeUtil.generate("https://hutool.cn/", 300, 300);


		FileInfo f = fileInfoService.getById(id);
		return AjaxJson.getSuccessData(f);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	@RequestMapping("getList")
	@SaCheckPermission(FileInfo.PERMISSION_CODE)
	public AjaxJson getList() {
		SoMap so = SoMap.getRequestSoMap();
		List<FileInfo> list = fileInfoService.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	@RequestMapping("getListNoPage")
	public AjaxJson getListNoPage(String type) {
		List<FileInfo> list = fileInfoService.getListNoPage(type);
		return AjaxJson.getSuccessData(list);
	}

	/** 改 - 状态(1=使用,2=禁用) */
	@RequestMapping("updateStatus")
	@SaCheckPermission(FileInfo.PERMISSION_CODE)
	public AjaxJson updateStatus(Integer id, Integer value){
		int line = SP.publicMapper.updateColumnById(FileInfo.TABLE_NAME, "status", value, id);
		return AjaxJson.getByLine(line);
	}


	// ------------------------- 前端接口 -------------------------


	/** 改 - 不传不改 [G] */
	@RequestMapping("updateByNotNull")
	public AjaxJson updateByNotNull(Integer id){
		AjaxError.throwBy(true, "如需正常调用此接口，请删除此行代码");
		// 鉴别身份，是否为数据创建者
		long userId = SP.publicMapper.getColumnByIdToLong(FileInfo.TABLE_NAME, "user_id", id);
		AjaxError.throwBy(userId != StpUserUtil.getLoginIdAsLong(), "此数据您无权限修改");
		// 开始修改 (请只保留需要修改的字段)
		SoMap so = SoMap.getRequestSoMap();
		so.clearNotIn("id", "name", "fileUrl", "type", "note", "status", "createTime", "rootFolder").clearNull().humpToLineCase();
		int line = SP.publicMapper.updateBySoMapById(FileInfo.TABLE_NAME, so, id);
		return AjaxJson.getByLine(line);
	}


	@RequestMapping("rabbitMq")
	public void rabbitMq(HttpServletRequest request, HttpServletResponse response) throws IOException {


		response.setHeader("Accept","*/*");
		response.setHeader("Accept-Encoding","gzip, deflate");
		response.setHeader("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
		response.setHeader("authorization","Basic YWRtaW46YWRtaW4=");
		response.setHeader("Connection","keep-alive");
		response.setHeader("content-type","application/json");
//		response.setHeader("Cookie","m=2258:YWRtaW46YWRtaW4%253D");
		response.setHeader("Host","47.108.13.3:15672");
		response.setHeader("Referer","http://47.108.13.3:15672/");
		response.setHeader("User-Agent","	Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0");
		Cookie cookie = new Cookie("m", "2258:YWRtaW46YWRtaW4%253D");
		cookie.setMaxAge(3600);
		cookie.setPath("/");
		cookie.setDomain("http://47.108.13.3:15672");
		response.addCookie(cookie);
		response.sendRedirect("http://47.108.13.3:15672/#/users");
		System.out.println("rabbitMq");
	}

	@RequestMapping("activeMq")
	public void activeMq(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("authorization","Basic YWRtaW46YWRtaW4=");
		response.setHeader("Cookie","m=2258:YWRtaW46YWRtaW4%253D");
		response.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.36");
		response.sendRedirect("http://47.108.13.3:15672/#/users");
		System.out.println("activeMq");
	}


}
